﻿using Super_Management_Mayhem.Content.Spritesheet;
using Transformable_Engine_v2.Engine.Animation.ScaleAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Menu.SelectIcon
{
    public class AvatarIconMenu : BaseMenuScreen<MainMenu.Menu>
    {
        private const int SELECTION_TIME = 200;
        private const int VISIBLE_LEVELS = 3;
        private const int WHEEL_RADIUS = 12;

        private ItemWheel _wheel;

        public AvatarIconMenu(TE_Vector2 size, int selectedIndex)
        {
            int itemCount = (int) ProfileIconSheet.Icon.Count;
            var icons = new ImageContainer[itemCount];
            for (int i = 0; i < itemCount; i++)
            {
                icons[i] = new ImageContainer {
                                                  Sprite = ProfileIconSheet.GetSprite((ProfileIconSheet.Icon) i),
                                                  Origin = TE_Rectangle.Alignment.CenterMiddle,
                                                  Scale = new TE_Vector2(2, 2)
                                              };
            }

            var cursorImage = new ImageContainer {
                                                     Sprite = MenuSheet.GetSprite(MenuSheet.Menu.LetterSelected),
                                                     Origin = TE_Rectangle.Alignment.CenterMiddle,
                                                     Scale = new TE_Vector2(2.25f, 2.25f)
                                                 };
            cursorImage.AddAnimations(new ScaleAnimation(new SinusTrajectory(), new TE_Vector2(2.75f, 2.75f), 800, true, 0));

            _wheel = new ItemWheel(selectedIndex, SELECTION_TIME, VISIBLE_LEVELS, WHEEL_RADIUS, cursorImage, icons) {
                                                                                        PositionGridBound = new TE_Vector2(size.X / 2, size.Y / 2 + WHEEL_RADIUS)
                                                                                    };

            var title = Title.Blue("Select a character icon", size);

            AddChildren(_wheel, title);
        }

        public int CurrentIcon
        {
            get
            {
                return _wheel.SelectedItemIndex;
            }
        }


        public override void UpKey(int id)
        {
        }

        public override void DownKey(int id)
        {
        }

        public override void LeftKey(int id)
        {
            _wheel.CycleLeft();
        }

        public override void RightKey(int id)
        {
            _wheel.CycleRight();
        }

        public override void AcceptKey(int id)
        {
            ChangeScreen(-1, MainMenu.Menu.Waiting);
        }

        public override void CancelKey(int id)
        {
            ChangeScreen(-1, MainMenu.Menu.CreateNewAvatar_Color);
        }
    }
}